package junior.图;

public class 图中最大的岛屿 {
    static int N = 6;
    static int MaxLand = 0;
    static int TemtMaxLand = 0;
    static int[][] check = {{0,0,0,0,1,0},
                            {0,1,0,0,1,0},
                            {0,1,0,0,1,0},
                            {0,1,0,1,0,0},
                            {0,1,0,1,0,1},
                            {0,0,1,0,1,0}};
    static int[][] direction = {{0 ,1 },
                                {1 ,0 },
                                {-1,0 },
                                {0 ,-1}}; //右下左上
    static int m=6, n=6; //m行n列
    public static void main(String[] args) {
        MaxLand = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if(check[i][j] == 1){
                    TemtMaxLand = 0;
                    recurision(i,j);
                    MaxLand = Math.max(MaxLand,TemtMaxLand);
                }
            }
        }
        System.out.println("最大的岛屿：" + MaxLand);
    }
    static void recurision(int x, int y){  // x 行 ， y 列
        int newX,newY;
        check[x][y] = 2; // 保存结果,check[][]值设置为2, 打标记
        TemtMaxLand++;
        for (int i = 0; i < 4; i++) { // 遍历四个方向
            newX = x+direction[i][0];
            newY = y+direction[i][1];
            if( 0 <= newX && newX < m && 0 <= newY && newY < n && check[newX][newY] == 1) // 判断：边界，是否为1（强）
                    recurision(newX,newY); // 递归
        }
    }
}
